package com.shangma.exercise;

import java.util.Arrays;
import java.util.Scanner;

/**
 * @classname:demo
 * @description:
 * @author: Mr.Z
 * @date: 2020/11/26 16:38
 * @version: 0.1
 * @since:1.8
 */
public class demo {
    public static void main(String[] args) {
//      demo01();
//      demo02();
//        demo03();
//        demo04();
//        demo05();
//        demo06();
    }

    private static void demo06() {
//        从键盘接受10个整数保存在数组中，将10个数中最大的与第一个元素交换
//        最小的与最后一个元素交换，其余元素位置不变，输出数组元素的值
        Scanner input = new Scanner(System.in);
        int[] sums = new int[10];
        for (int i = 0, len = sums.length; i < len; i++) {
            System.out.println("请输入第" + (i + 1) + "个元素");
            sums[i] = input.nextInt();
        }
        System.out.println("原数组为\t\t\t\t\t" + Arrays.toString(sums));
        input.close();
        int max = sums[0];
        int maxIndex = 0;
        for (int i = 0, len = sums.length; i < len; i++) {
            if (sums[i] > max) {
                max = sums[i];
                maxIndex = i;
            }
        }
        int temp = sums[maxIndex];
        sums[maxIndex] = sums[0];
        sums[0] = temp;
        System.out.println("当最大的数值与第一位交换:" + Arrays.toString(sums));
        int min = sums[0];
        int minIndex = 0;
        for (int i = 0, len = sums.length; i < len; i++) {
            if (sums[i] < min) {
                min = sums[i];
                minIndex = i;
            }
        }
        int an = sums[minIndex];
        sums[minIndex] = sums[9];
        sums[9] = an;
        System.out.println("当最小的数值与最后一位交换:" + Arrays.toString(sums));

    }


    private static void demo05() {
//        Int[] array = {2,432,5221,235,2,5352,1,53,5,3,5364,2,2,63,3533,2,53,532,532};
//        求所有元素和。
//        输出所有奇数下标元素。如：array[1]
//        输出所有元素中，值为奇数的。
//        将所有元素乘二。
//        将所有元素加到第一个元素中。
//        将奇数位置元素存到B数组中
//        偶数元素存到C数组中
//        分别逆序输出B数组和C数组
        int[] array = {2, 432, 5221, 235, 2, 5352, 1, 53, 5, 3, 5364, 2, 2, 63, 3533, 2, 53, 532, 532};
        int[] array1 = {2, 432, 5221, 235, 2, 5352, 1, 53, 5, 3, 5364, 2, 2, 63, 3533, 2, 53, 532, 532};

        int sum = 0;
        for (int i = 0, len = array.length; i < len; i++) {
            sum += array[i];
        }
        System.out.println("所有元素的和为：" + sum);
        System.out.print("输出所有奇数下标元素");
        for (int i = 0; i < array.length; i++) {
            if ((array[i] % 2) != 0) {
                System.out.print("array[" + i + "]" + "\t");
            }
        }
        System.out.println();

        System.out.print("输出所有奇数元素");
        for (int i = 0; i < array.length; i++) {
            if ((array[i] % 2) != 0) {
                System.out.print(array[i] + "\t");
            }
        }
        System.out.println();

        for (int i = 0; i < array.length; i++) {
            array[i] = array[i] * 2;
        }
        System.out.println("将所有元素乘二后:" + Arrays.toString(array));

        for (int i = 1; i < array.length; i++) {
            array[0] += array[i];
        }
        System.out.println("将所有元素加到第一个元素中后:" + Arrays.toString(array));
        int[] newArrays = new int[9];
        int index = 0;
        for (int i = 0; i < array1.length; i++) {
            if ((array1[i] % 2) != 0) {
                newArrays[index] = array1[i];
                index++;
            }
        }
        System.out.println("（奇数）新数B组为" + Arrays.toString(newArrays));
        int count = 0;
        for (int i = 0; i < array1.length; i++) {
            if ((array1[i] % 2) == 0) {
                count++;
            }
        }
        int[] newArrays2 = new int[count];
        int index1 = 0;
        for (int i = 0; i < array1.length; i++) {
            if ((array1[i] % 2) == 0) {
                newArrays2[index1] = array1[i];
                index1++;
            }
        }
        System.out.println("（偶数）新数C组为" + Arrays.toString(newArrays2));
        System.out.print("倒序输出B数组为");
        for (int i = newArrays.length - 1; i >= 0; i--) {
            System.out.print(newArrays[i] + "\t");
        }
        System.out.println();
        System.out.print("倒序输出C数组为");
        for (int i = newArrays2.length - 1; i >= 0; i--) {
            System.out.print(newArrays2[i] + "\t");
        }


    }

    private static void demo04() {
//        现在有如下一个数组： int[] oldArr={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5};
//        要求将以上数组中的0项去掉，将不为0的值存入一个新的数组，生成新的数组为
//        Int[] newArr={1,3,4,5,6,6,5,4,7,6,7,5};
        int[] oldArr = {1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5};
        int count = 0;
        for (int i = 0, len = oldArr.length; i < len; i++) {
            if (oldArr[i] != 0) {
                count++;
            }
        }
        int[] newArr = new int[count];
        int index = 0;
        for (int i = 0, len = oldArr.length; i < len; i++) {
            if (oldArr[i] != 0) {
                newArr[index] = oldArr[i];
                index++;
            }
        }
        System.out.println(Arrays.toString(newArr));

    }

    private static void demo03() {
        //声明一个int型的数组，循环接收8个学生的成绩，
        // 计算这8个学生的总分、平均分、最高分、最低分

        int[] scores = new int[8];
        Scanner input = new Scanner(System.in);
        for (int i = 0; i < 8; i++) {
            System.out.println("请输入第" + (i + 1) + "名同学的成绩");
            scores[i] = input.nextInt();
        }
        input.close();
        for (int tian = scores.length - 1; tian >= 0; tian--) {
            int max = scores[0];
            int maxIndex = 0;
            for (int j = 0; j <= tian; j++) {
                if (max < scores[tian]) {
                    max = scores[j];
                    maxIndex = j;
                }
            }
            int temp = scores[maxIndex];
            scores[maxIndex] = scores[tian];
            scores[tian] = temp;
        }
        int sum = 0;
        for (int score : scores) {
            sum += score;
        }
        System.out.println("总分数为:" + sum);
        System.out.println("最高分为：" + scores[7]);
        System.out.println("最低分为：" + scores[0]);
        System.out.println("平均分为：" + sum / 8);


    }


    private static void demo02() {
        //声明一个字符串的数组，空间为5个。使用循环接收五个学生的姓名。再使用循环输出这五 个学生的姓名
        Scanner input = new Scanner(System.in);
        String[] stuName = new String[5];
        for (int i = 0; i < 5; i++) {
            System.out.println("请输入第" + (i + 1) + "个学生的姓名");
            stuName[i] = input.next();
        }
        input.close();
        for (String name : stuName) {
            System.out.print(name + "\t");
        }

    }

    private static void demo01() {
        //        在歌唱比赛中，共有10位评委进行打分，在计算歌手得分时，
//        去掉一个最高分，去掉一个最低分，然后剩余的8位评委的分数进行平均
//        就是该选手的最终得分。输入每个评委的评分，求某选手的得分
        int[] arrays = new int[10];
        Scanner input = new Scanner(System.in);
        for (int i = 0, len = arrays.length; i < len; i++) {
            System.out.println("请输入第" + (i + 1) + "位评委的评分");
            arrays[i] = input.nextInt();
        }
        input.close();
        for (int i = 0, len = arrays.length; i < len; i++) {
            for (int j = 0; j < len - i - 1; j++) {
                if (arrays[j] > arrays[j + 1]) {
                    int temp = arrays[j];
                    arrays[j] = arrays[j + 1];
                    arrays[j + 1] = temp;
                }
            }
        }
        arrays[0] = 0;
        arrays[9] = 0;
        int sum = 0;
        for (int i = 0, len = arrays.length; i < len; i++) {
            sum += arrays[i];
        }
        System.out.println("该选手的得分为" + sum / 8);

    }
}
